package com.peration.cropdiagnosis;

import java.util.ArrayList;
import java.util.Hashtable;

import com.peration.connect.Sender;
import com.peration.model.Item;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.opengl.Visibility;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.SyncStateContract.Constants;
import android.telephony.TelephonyManager;
import android.text.InputType;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView.LayoutParams;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class Diagnosis extends Activity implements OnClickListener {
	ArrayList<Item> msgList = new ArrayList<Item>();
	String sessionId;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.diagnosis);

		((Button) findViewById(R.id.button_diag)).setOnClickListener(this);

		((TextView) findViewById(R.id.diagType)).setText("Diagnosis type: "
				+ DiagTypeSelect.CurrentDiagType);
		populateList();
	}

	private void populateList() {
		TableLayout tbl = (TableLayout) findViewById(R.id.signs_list);
		TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
				TableLayout.LayoutParams.FILL_PARENT,
				TableLayout.LayoutParams.WRAP_CONTENT);
		TableRow.LayoutParams para = new TableRow.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		int leftMargin = 0;
		int topMargin = 1;
		int rightMargin = 0;
		int bottomMargin = 1;
		tableRowParams.setMargins(leftMargin, topMargin, rightMargin,
				bottomMargin);
		para.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
		TableRow newRow;
		TextView myText;
		tbl.removeAllViews();
		newRow = new TableRow(this);
		newRow.setLayoutParams(tableRowParams); // left,top,right, bottom
		// space before table

		// titles borders
		myText = new TextView(this);
		myText.setText("");
		myText.setBackgroundColor(Color.GRAY);
		myText.setTextColor(Color.WHITE);
		myText.setLayoutParams(para);
		// myText.setVisibility(View.INVISIBLE);
		newRow.addView(myText);

		myText = new TextView(this);
		myText.setText("Signs");
		myText.setBackgroundColor(Color.GRAY);
		myText.setTextColor(Color.WHITE);
		myText.setLayoutParams(para);
		newRow.addView(myText);
		// align right
		myText = new TextView(this);
		myText.setText("Observed?");
		myText.setBackgroundColor(Color.GRAY);
		myText.setTextColor(Color.WHITE);
		myText.setLayoutParams(para);
		newRow.addView(myText);

		tbl.addView(newRow);
		int count = 0;

		for (Item item : msgList) {
			count++;
			Log.i("Adding to table:", count + ", " + item.symptom + ", "
					+ item.observed);
			newRow = new TableRow(this);
			newRow.setLayoutParams(tableRowParams); // left,top,right, bottom

			myText = new TextView(this);
			myText.setText(count + "");
			myText.setBackgroundColor(Color.WHITE);
			myText.setTextColor(Color.BLACK);
			myText.setLayoutParams(para);
			newRow.addView(myText);

			myText = new TextView(this);
			myText.setText(wrap(item.symptom, 30));
			myText.setBackgroundColor(Color.WHITE);
			myText.setTextColor(Color.BLACK);
			myText.setLayoutParams(para);
			// myText.setVisibility(View.INVISIBLE);
			newRow.addView(myText);

			myText = new TextView(this);
			myText.setText(item.observed + "");
			myText.setBackgroundColor(Color.WHITE);
			myText.setTextColor(Color.BLACK);
			myText.setLayoutParams(para);
			newRow.addView(myText);
			tbl.addView(newRow);
		}

		// ((TextView) findViewById(R.id.diagnosis))
		// .setText("Diagnosis not yet made?");
	}

	/*
	 * @Override protected void onListItemClick(ListView l, View v, int
	 * position, long id) { super.onListItemClick(l, v, position, id); // Get
	 * the item that was clicked Object o = myAdapt.getItem(position); String
	 * keyword = o.toString(); Toast.makeText(this, "You selected: " + keyword,
	 * Toast.LENGTH_LONG) .show(); }
	 */

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_diag:
			sessionId = Sender.fetchUrl("new?diagType="
					+ DiagTypeSelect.CurrentDiagType);
			msgList = new ArrayList<Item>();
			startDiagnosis("", "");
			break;
		}
	}

	private void startDiagnosis(String prevSymptom, String answer) {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		final String symptom = Sender.fetchUrl("diagnosis?id=" + sessionId
				+ "&symptom=" + prevSymptom + "&answer=" + answer);

		if (symptom.equals("end")) {
			((TextView) findViewById(R.id.diagnosis))
					.setText("Diagnosis could not be made");
		} else if (symptom.startsWith("found:")) {
			((TextView) findViewById(R.id.diagnosis)).setText(symptom
					.substring("found: ".length()));
		} else {
			alert.setTitle("Diagnosis");

			alert.setMessage(symptom + "?");

			alert.setPositiveButton("Ok",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							msgList.add(new Item(symptom + "?", "Yes"));
							populateList();
							startDiagnosis(symptom, "yes");
						}
					});

			alert.setNegativeButton("Cancel",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							msgList.add(new Item(symptom + "?", "No"));
							populateList();
							startDiagnosis(symptom, "no");
						}
					});

			alert.show();
		}

	}

	private String wrap(String text, int size) {
		String res = "";
		while (text.length() > 0) {
			if (text.length() > size) {
				res += text.substring(0, size) + "\n";
				text = text.substring(size);
			} else {
				res += text;
				text = "";
			}
		}
		return res;
	}
}
